/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';

const Controller = require('egg').Controller;
const { v4: uuidv4 } = require('uuid');

const moment = require('moment');

class ArticleController extends Controller {
  async create() {
    const { ctx } = this;
    const params = {
      ...ctx.request.body,
      createTime: moment().format('YYYY-MM-DD HH:mm:ss'),
      id: uuidv4(),
    };
    const result = await ctx.service.article.create(params);
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: 'err',
      };
    }
  }
  async updatePost() {
    const { ctx } = this;
    let IdInUrl = ctx.request.url.split('/');
    IdInUrl = IdInUrl[IdInUrl.length - 1];
    const params = {
      ...ctx.request.body,
      id: IdInUrl,
    };
    const result = await ctx.service.article.updatePost(params);
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          data: result,
        },
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: 'err',
      };
    }
  }
  async lists() {
    const { ctx } = this;
    const result = await ctx.service.article.lists();
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: '查询文章列表失败',
      };
    }
  }
  async detail() {
    const { ctx } = this;
    const result = await ctx.service.article.detail(ctx.params.id);
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: '该文章不存在',
      };
    }
  }
  async posts() {
    const { ctx } = this;
    const result = await ctx.service.article.posts(ctx.params.id);
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: '该专栏不存在',
      };
    }
  }
  async DetailPosts() {
    const { ctx } = this;
    const result = await ctx.service.article.postDetail(ctx.params.id);
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: '该专栏不存在',
      };
    }
  }
  async deletePosts() {
    const { ctx } = this;
    const result = await ctx.service.article.deletePost(ctx.params.id);
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
    } else {
      ctx.body = {
        status: 500,
        errMsg: '删除失败',
      };
    }
  }
}

module.exports = ArticleController;
